<?php
/**
 * 自定义表单 - 前台操作类
 * @author           吉他郭  
 * @license          http://www.d163.net/
 * @lastmodify       2018-01-15
 */
 
 
defined('IN_GUOPHP') or exit('Access Denied'); 

class index{
	
	public $modelid, $modelinfo;
	public function __construct() {
		
		$this->modelid = isset($_GET['modelid']) ? intval($_GET['modelid']) : (isset($_POST['modelid']) ? intval($_POST['modelid']) : 0);
		if(ROUTE_A != 'index')	$this->_check_model();
	}

	
	/**
	 * 自定义表单列表
	 */	
	public function index(){	
		andy_base::load_sys_class('page','',0);
		$model = D('model');
		$total = $model->where(array('type'=>1, 'disabled'=>0))->total();
		$page = new page($total, 15);
		$formdata = $model->where(array('type'=>1, 'disabled'=>0))->order('modelid DESC')->limit($page->limit())->select();
		
		//SEO相关设置
		$site = get_config();
		$seo_title = '自定义表单列表_'.$site['site_name'];
		$keywords = $site['site_keyword'];
		$description = $site['site_description'];
		$pages = '<span class="pageinfo">共'.$total.'条记录</span>'.$page->getfull();
		include T('list_diyform' ,'index');	
	}
	
	
	
	/**
	 * 自定义表单显示页
	 */	
	public function show(){
		$modelid = $this->modelid;
		$modelinfo = $this->modelinfo;
		$title =  $modelinfo['name'];
		$template = $modelinfo['show_template'];
		
		//SEO相关设置
		$site = get_config();
		$seo_title = $title.'_'.$site['site_name'];
		$keywords = $site['site_keyword'];
		$description = $modelinfo['description'] ? $modelinfo['description'] : $site['site_description'];
		
		//获取当前位置
		$location = '<a href="'.SITE_URL.'">首页</a> &gt; <a href="'.U('index').'">'.$modelinfo['name'].'</a> &gt;显示页';
		$fieldstr = $this->_get_model_str($modelid);
		$field_check = $this->_get_model_str($modelid, true);
		include T($template, 'index');	
	}
	
	
	
	/**
	 * 自定义表单提交
	 */	
	public function post(){	
		$allow_origin = array(
            'http://www.d525666.com', //注意加上http或https
            'https://d525.gitee.io',
            'http://b.baidu.com',
        );
        //跨域访问的时候才会存在此字段
        $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';  
        if (in_array($origin, $allow_origin)) {
            header('Access-Control-Allow-Origin:'.$origin);
        }

		if(isset($_POST['dosubmit'])){

			if($this->modelinfo['check_code']){
				if(empty($_SESSION['code']) || strtolower($_POST['code'])!=$_SESSION['code']){
					$_SESSION['code'] = '';
					return_json(array('status'=>0,'message'=>'验证码错误','data'=>array()));
				}
				$_SESSION['code'] = '';
			}

			$field_check = $this->_get_model_str($this->modelid, true);
			foreach($field_check as $k => $v){
				if($v['isrequired']){
					if(empty($_POST[$k])) {
					   return_json(array('status'=>0,'message'=>$v['errortips'],'data'=>array())); 
					}
					
				}
			}

			foreach($_POST as $_k=>$_v) {
				$_POST[$_k] = !is_array($_POST[$_k]) ? new_html_special_chars(trim_script($_v)) : $this->_content_dispose($_v);
			}
			
			$_POST['userid'] = isset($_SESSION['_userid']) ? $_SESSION['_userid'] : 0;
			$_POST['username'] = isset($_SESSION['_username']) ? $_SESSION['_username'] : '';
			$_POST['ip'] = getip();
			$_POST['inputtime'] = SYS_TIME;
            $tablename = D($this->modelinfo['tablename']);

            if(in_array($this->modelid, array('5'))){
            	$domain = $_POST['domain'] = isset($_POST['domain']) ? safe_replace($_POST['domain']) : '';
                if(strpos($domain,'.')===false) {
                    return_json(array("status"=>0,"message"=>'请输入有效的域名！',"data"=>array()));
                }
                $authcode = $_POST['authcode'] = $this->_creat_authcode($domain);

                $res = $tablename->where(array('domain' => $_POST['domain']))->find();

                if($res){
           	    	return_json(array("status"=>1,"message"=>'获取成功',"data"=>array("domain"=>$domain,"authcode"=>$res['authcode'])));
                }
            }
            
            if(in_array($this->modelid, array('6'))){
        		$res = $tablename->where(array('uid' => $_POST['uid']))->find();
        		if($res){
	        		return_json(array("status"=>1,"message"=>'提交成功,重复数据'));
        		}
            }	
			
			$id = $tablename->insert($_POST);
			
			if(!$id) showmsg(L('operation_failure'), 'stop');
			D('model')->update('`items`=`items`+1', array('modelid'=>$this->modelid));

			//发送邮件通知
			if($this->modelinfo['sendmail']){
				sendmail(get_config('mail_inbox'), '表单消息提醒：', 
				'您的网站-表单（'.$this->modelinfo['name'].'）有新的消息，<a href="'.get_config('site_url').'">请查看</a>！<br> <b>'.get_config('site_name').'</b>');
			}

			if(in_array($this->modelid, array('5'))){
				return_json(array("status"=>1,"message"=>'获取成功',"data"=>array("domain"=>$domain,"authcode"=>$authcode)));
			}
			
			return_json(array("status"=>1,"message"=>'提交成功'));
			
		}
	}
	
	/**
	 * 检查model
	 */	
	private function _check_model() {
		new_session_start();
		$data = D('model')->where(array('modelid'=>$this->modelid))->find();
		if(!$data || $data['type']!=1 || $data['disabled']==1){
			showmsg('表单不存在或已禁用!', 'stop');
		}
		
		$setting = json_decode($data['setting'], true);
		if(!$setting['allowvisitor'] && empty($_SESSION['_userid'])) showmsg('请登录会员！', url_referer(get_url()), 2);
		
		$this->modelinfo = array_merge($data, $setting);
	}
	
	
    private function _creat_authcode($domain) {
        $domain = safe_replace($domain);
        if(in_array($domain, array('www.baidu.com','baidu.com','qq.com','www.qq.com','163.com','www.163.com','jd.com','www.jd.com','mi.com','www.mi.com','1688.com','www.1688.com','taobao.com','www.taobao.com'))){
            return_json(array("status"=>0,"message"=>'此域名太牛逼了，您还是换个域名吧！',"data"=>array()));
        }
        $dnum = substr_count($domain,'.');
        $arr = explode('.', $domain);
        if(in_array('', $arr)){
            return_json(array("status"=>0,"message"=>'请输入有效的域名！',"data"=>array()));
        }
        $okdomain_arr = array('com','cn','net','cc','top','vip','info','xin','club','xyz','ltd','co','wang','beer','art','cloud','site','shop','fun','link','online','tech','ren','luxe','pro','kim','work','red','ink','group','store','host','host','pub','live','wiki','design','video','fit','yoga','biz','org','name','me','tv','mobi','asia','pw','pics','bid','gift','photo','men','news','win','party','date','website','space','press','band','engineer','market','social','software','studio','mom','lol','hk','tel','game','games','help','so','love','plus','today','gold','city','show','run','world','icu','life','fans','bar','cool','zone','chat','company','law','fund','email','team','center','guru');
        if(!in_array(end($arr), $okdomain_arr)){
        	return_json(array("status"=>0,"message"=>'请输入有效的域名！',"data"=>array()));
        }
        if($dnum == 1){
            return $this->_ccode($domain).','.$this->_ccode('www.'.$domain);
        }
        if($dnum == 2){
            $arr = explode('.', $domain);
            if($arr[0]=='www'){
                return $this->_ccode($domain).','.$this->_ccode($arr[1].'.'.$arr[2]);
            }
        }
        return $this->_ccode($domain);
    }
    
    private function _ccode($a){
        $domain = safe_replace($a);
        return strtoupper(substr(md5(substr(sha1($domain), 1, 10)), 10, 10));
    }

	/**
	 * 内容处理
	 * @param $content 
	 */	
	private function _content_dispose($content) {
		$is_array = false;
		foreach($content as $val){
			if(is_array($val)) $is_array = true;
			break;
		}
		if(!$is_array) return safe_replace(implode(',', $content));
		
		//这里认为是多文件上传
		$arr = array();
		foreach($content['url'] as $key => $val){
			$arr[$key]['url'] = safe_replace($val);
			$arr[$key]['alt'] = safe_replace($content['alt'][$key]);
		}		
		return array2string($arr);
	}

	
	
	/**
	 * 获取不同模型获取HTML表单
	 */	
	private function _get_model_str($modelid, $field = false, $data = array()) {
		andy_base::load_sys_class('form','',0);
		$modelinfo = getcache($modelid.'_model');
		if($modelinfo === false){
			$modelinfo = D('model_field')->where(array('modelid' => $modelid, 'disabled' => 0))->order('listorder ASC, fieldid ASC')->select();
			setcache($modelid.'_model', $modelinfo);
		}
		
		$fields = $fieldstr = array();
		foreach($modelinfo as $val){
			$fieldtype = $val['fieldtype'];
			if($data){
				$val['defaultvalue'] = isset($data[$val['field']]) ? $data[$val['field']] : '';
			}
			$setting = $val['setting'] ? string2array($val['setting']) : 0;
			$required = $val['isrequired'] ? '<span class="red">*</span>' : '';
			$fieldstr[] = '<td class="yzm-table-title">'.$val['name'].$required.'</td><td>'.form::$fieldtype($val['field'], $val['defaultvalue'], $setting).'</td>';	
			$fields[$val['field']] = $val['isrequired'] ? array('isrequired'=>1, 'fieldtype'=>$fieldtype, 'errortips'=>$val['errortips'] ? $val['errortips'] : $val['name'].'不能为空！') : array('isrequired'=>0);
		}
		
		return $field ? $fields : $fieldstr;
	}

}
